<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2022/3/14
 * Time: 9:40
 */

namespace app\api\controller;



use think\Db;

class Boindex extends Base
{
    private $user_id; // 用户ID
    private $page; // 页码
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id, $time, $value, $reqKey);
        if($auth == 112){
            echo json_encode(['status' => 301, 'msg' => $this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }

    /**
     * 推荐礼包
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function recommendGiftbag(){
        $gift = Db::table('up_bo_giftbag')
            ->where(['gift_is_recommend' => 1, 'gift_status' => 1])
            ->field('gift_id, gift_name, gift_images, gift_price, gift_indate')
            ->limit(5)
            ->select();
        if($gift){
            foreach ($gift as $k => $v){
                if($v['gift_images']){
                    $gift[$k]['gift_images'] = explode(',', $v['gift_images']);
                }else{
                    $gift[$k]['gift_images'] = [];
                }
            }
            return json(['status' => 200, 'msg' => $gift]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }

    }
    
    /**
     * 商品分类
     * @return \think\response\Json
     */
    public function goodsCate(){
        $fcate = Db::table('up_bo_goods_cate')->where('bgc_level', 1)->field('bgc_id, bgc_name')->order('bgc_sort desc')->select();
        if($fcate){
            foreach($fcate as $k => $v){
                $scate = Db::table('up_bo_goods_cate')->where('bgc_pid', $v['bgc_id'])->field('bgc_id, bgc_name')->order('bgc_sort desc')->select();
                if($scate){
                    $fcate[$k]['son'] = $scate;
                }else{
                    $fcate[$k]['son'] = [];
                }
            }
            return json(['status' => 200, 'msg' => $fcate]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 产品中心
     * keyword       搜素关键字
     * first_cate    一级分类
     * second_cate   二级分类
     * is_recommend  0 不参与   1 不推荐   2 推荐
     * goods_type    1 花样青青   2 脖脸皮
     * @return \think\response\Json
     */
    public function goodsCentre(){
        $keyword = trim(input('keyword'));
        $second_cate = input('second_cate') ? input('second_cate') : 0;
        $first_cate = input('first_cate') ? input('first_cate') : 0;
        $is_recommend = input('is_recommend') ? input('is_recommend') : 0; // 0 不参与  1 不推荐  2 推荐
        $goods_type = input('goods_type') ? input('goods_type') : 0; // 0 不区分 1 花样青青   2 脖脸皮

        $goods = $this->goodsList($keyword, $first_cate, $second_cate, $is_recommend, $goods_type);
        if(!$goods) $goods = [];
        return json(['status' => 200, 'msg' => $goods]);
    }

    /**
     * 推荐产品列表
     * keyword       搜素关键字
     * page          页码
     * @return \think\response\Json
     */
    public function recommendGoods(){
        $keyword = trim(input('keyword'));
        $where['bg_status'] = 1;
        $where['bg_is_commend'] = 2;
        if($keyword != ''){
            $where['bg_name'] = ['like', '%' . $keyword . '%'];
        }
        $goods = Db::table('up_bo_goods')
            ->where($where)
            ->field('bg_id, bg_name, bg_thumb, bg_spec, bg_stock, bg_price, bg_sale_number, 
            bg_show_salenumber, bg_show_stock')
            ->order('bg_sort desc, bg_time desc')
            ->page($this->page, 10)
            ->select();
        if(!$goods) $goods = [];
        return json(['status' => 200, 'msg' => $goods]);
    }
    
    /**
     * 商品详情
     * @return \think\response\Json
     * bg_id     商品ID
     */
    public function goodsDetail(){
        $bg_id = input('bg_id');
        $detail = Db::table('up_bo_goods')
            ->where('bg_id', $bg_id)
            ->field('bg_id, bg_name, bg_price, bg_spec, bg_stock, bg_sale_number, bg_images, bg_content, 
            bg_status, bg_thumb, bg_show_salenumber, bg_show_stock, bg_old_price')
            ->find();
        if($detail['bg_status'] != 1) return json(['status' => 0, 'msg' => '商品状态异常']);
        if($detail['bg_images']){
            $detail['bg_images'] = explode(',', $detail['bg_images']);
        }else{
            $detail['bg_images'] = [];
        }
        // 是否收藏
//        $is_collect = $this->isCollectGoods($bg_id);
//        $detail['is_collect'] = $is_collect;
        // 购物车数量
        $shopCar_number = $this->myShopCar(1);
        $detail['shopCar_number'] = $shopCar_number;
        return json(['status' => 200, 'msg' => $detail]);
    }

    /**
     * 脖脸皮商品详情---用于H5
     * @return \think\response\Json
     *
     */
    public function goodsDetailForH5(){
        header("Access-Control-Allow-Origin: *");
        $bg_id = input('bg_id');
        $detail = Db::table('up_bo_goods')
            ->where('bg_id', $bg_id)
            ->field('bg_id, bg_name, bg_spec, bg_images, bg_content, bg_thumb')
            ->find();
        if($detail['bg_images']){
            $detail['bg_images'] = explode(',', $detail['bg_images']);
        }else{
            $detail['bg_images'] = [];
        }
        return json(['status' => 200, 'msg' => $detail]);
    }
    
    /**
     * 收藏/取消收藏
     * @return \think\response\Json
     * bg_id      商品ID
     */
    public function doCollect(){
        $bg_id = input('bg_id');
        $collect = Db::table('up_bo_collect_goods')
            ->where(['bcg_user_id' => $this->user_id, 'bcg_goods_id' => $bg_id])
            ->field('bcg_id')
            ->find();
        if($collect){
            $res = Db::table('up_bo_collect_goods')->delete($collect['bcg_id']);
        }else{
            $res = Db::table('up_bo_collect_goods')
                ->insert([
                    'bcg_user_id' => $this->user_id,
                    'bcg_goods_id' => $bg_id,
                    'bcg_time' => $this->create_time
                ]);
        }
        if($res){
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
        }
    }
    
    /**
     * 商品基本信息
     * @return \think\response\Json
     * bg_id      商品ID
     */
    public function goodsBaseInfo(){
        $bg_id = input('bg_id');
        $goods = Db::table('up_bo_goods')
            ->where('bg_id', $bg_id)
            ->field('bg_name, bg_thumb, bg_price, bg_old_price')
            ->find();
        return json(['status' => 200, 'msg' => $goods]);
    }
    
    /**
     * 加入购物车
     * @return \think\response\Json
     * bg_id        商品ID
     * goods_number  加入的商品数量
     */
    public function addShopCar(){
        $bg_id = input('bg_id');
        $goods_number = input('goods_number');
        $goods = Db::table('up_bo_goods')->where('bg_id', $bg_id)->field('bg_stock')->find();
        
        if($goods['bg_stock'] < $goods_number) return json(['status' => 0, 'msg' => '库存不足']);

        $user = Db::table('up_u_user')->where('user_id', $this->user_id)->field('user_bo_role, user_rule')->find();
        if($user['user_bo_role'] == 1 || $user['user_bo_role'] == 2 || $user['user_rule'] == '1' || $user['user_rule'] == '2'){
            return json(['status' => 0, 'msg' => '暂无购买权限']);
        }
        
        $in_shopCar = Db::table('up_bo_goods_car')
            ->where(['bgc_user_id' => $this->user_id, 'bgc_goods_id' => $bg_id])
            ->field('bgc_goods_number, bgc_id')
            ->find();
        if($in_shopCar){
            $res = Db::table('up_bo_goods_car')
                ->where('bgc_id', $in_shopCar['bgc_id'])
                ->setInc('bgc_goods_number', $goods_number);
        }else{
            $res = Db::table('up_bo_goods_car')
                ->insert([
                    'bgc_user_id' => $this->user_id,
                    'bgc_goods_id' => $bg_id,
                    'bgc_goods_number' => $goods_number,
                    'bgc_time' => $this->create_time
                ]);
        }
        if($res){
            return json(['status' => 200, 'msg' => '添加成功']);
        }else{
            return json(['status' => 0, 'msg' => '添加失败']);
        }
        
    }
    
    /**
     * 我的购物车列表
     * @return \think\response\Json
     * page     页码
     */
    public function shopCarList(){
        $list = $this->myShopCar(2);
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 增加购物车商品数量
     * @return \think\response\Json
     * bgc_id      购物车ID
     */
    public function incNum(){
        $bgc_id = input('bgc_id');
        $car = Db::table('up_bo_goods_car')
            ->join('up_bo_goods', 'bg_id = bgc_goods_id', 'LEFT')
            ->where('bgc_id', $bgc_id)
            ->field('bgc_goods_number, bg_stock')
            ->find();
        
        if($car['bgc_goods_number'] >= $car['bg_stock']) return json(['status' => 0, 'msg' => '超过最大库存']);
        
        $res = Db::table('up_bo_goods_car')->where('bgc_id', $bgc_id)->setInc('bgc_goods_number');
        if($res){
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 0, 'msg' => '操作异常，请稍后重试']);
        }
    }
    
    /**
     * 减少购物车商品数量
     * @return \think\response\Json
     * bgc_id     购物车ID
     */
    public function decNum(){
        $bgc_id = input('bgc_id');
        $car = Db::table('up_bo_goods_car')->where('bgc_id', $bgc_id)->field('bgc_goods_number')->find();
        if($car['bgc_goods_number'] <= 1) return json(['status' => 0, 'msg' => '商品数量不可小于1']);
        $res = Db::table('up_bo_goods_car')->where('bgc_id', $bgc_id)->setDec('bgc_goods_number');
        if($res){
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
        }
    }
    
    /**
     * 设置购物车商品数量
     * @return \think\response\Json
     * bgc_id      购物车ID
     * number      购物车商品数量
     */
    public function setNum(){
        $bgc_id = input('bgc_id');
        $number = input('number');
        
        if($number == 0) return json(['status' => 0, 'msg' => '商品数量不能小于1']);
        
        $car = Db::table('up_bo_goods_car')
            ->join('up_bo_goods', 'bg_id = bgc_goods_id', 'LEFT')
            ->where('bgc_id', $bgc_id)
            ->field('bg_stock')
            ->find();
        if($number > $car['bg_stock']) return json(['status' => 0, 'msg' => '库存不足']);
        
        $res = Db::table('up_bo_goods_car')
            ->where('bgc_id', $bgc_id)
            ->update([
                'bgc_goods_number' =>  $number,
                'bgc_time' => $this->create_time
            ]);
        if($res){
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
        }
    }
    
    /**
     * 删除购物车
     * @return \think\response\Json
     * bgc_ids      购物车ID
     */
    public function delShopCar(){
        $bgc_ids = input('bgc_ids');
        if(!$bgc_ids) return json(['status' => 0, 'msg' => '请选择需要删除的记录']);
        $res = Db::table('up_bo_goods_car')->where('bgc_id', 'IN', $bgc_ids)->delete();
        if($res){
            return json(['status' => 200, 'msg' => '删除成功']);
        }else{
            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
        }
    }
    
    /**
     * 立即购买提交页面展示
     * @return \think\response\Json
     * bg_id        商品ID
     * choose_number    选择的商品数量
     */
    public function payOneShow(){
        $bg_id = input('bg_id');
        $choose_number = input('choose_number');
        
        $goods = Db::table('up_bo_goods')
            ->where('bg_id', $bg_id)
            ->field('bg_name, bg_thumb, bg_price, bg_spec')
            ->find();
        // 快递公司
        $company = $this->expressCompany();
        $goods['company'] = $company;
        // 数量
        $goods['number'] = $choose_number;
        // 总金额
        $total_price = $choose_number * $goods['bg_price'];
        $goods['total_price'] = $total_price;
        // 剩余积分
        $user_balance = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_bo_balance');
        $goods['user_balance'] = $user_balance;
        
        return json(['status' => 200, 'msg' => $goods]);
    }
    
    /**
     * 购物车提交订单页面展示
     * @return \think\response\Json
     * car_ids   购物车ID，逗号隔开
     */
    public function payShopCarShow(){
        $car_ids = input('car_ids');
        if(!$car_ids) return json(['status' => 0, 'msg' => '请选择购物车商品']);
        $goods = Db::table('up_bo_goods_car')
            ->join('up_bo_goods', 'bg_id = bgc_goods_id', 'LEFT')
            ->where('bgc_id', 'IN', $car_ids)
            ->field('bg_id, bg_name, bg_thumb, bg_price, bg_spec, bgc_goods_number')
            ->select();
        $total_price = 0; // 总价格
        $total_number = 0; // 商品总数
        $return = [];
        foreach($goods as $k => $v){
            $total_price += $v['bgc_goods_number'] * $v['bg_price'];
            $total_number += $v['bgc_goods_number'];
        }
        $return['goods'] = $goods;
        // 快递公司
        $company = $this->expressCompany();
        $return['company'] = $company;
        // 总金额
        $return['total_price'] = $total_price;
        // 总数量
        $return['total_number'] = $total_number;
        // 剩余积分
        $user_balance = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_bo_balance');
        $return['user_balance'] = $user_balance;
        return json(['status' => 200, 'msg' => $return]);
    }
    
    /**
     * 立即购买--单件商品
     * @return \think\response\Json
     * bg_id       商品ID
     * addr_id     收货地址ID
     * goods_number   购买的商品数量
     * order_remark   订单备注
     */
    public function commitOneOrder(){
        $bg_id = input('bg_id');
        $addr_id = input('addr_id'); // 收货地址
        $goods_number = input('goods_number'); // 数量
        $order_remark = input('order_remark'); // 备注

        if(!$this->user_id) return json(['status' => 0, 'msg' => '请先登录']);
        if(!$addr_id) return json(['status' => 0, 'msg' => '请选择收货地址']);
        if($goods_number == 0) return json(['status' => 0, 'msg' => '购买的商品数量不能为0']);
        
        $goods = Db::table('up_bo_goods')
            ->where('bg_id', $bg_id)
            ->field('bg_price, bg_stock, bg_sale_number, bg_status')
            ->find();
        if($goods['bg_status'] != 1) return json(['status' => 0, 'msg' => '商品状态错误']);
        if($goods['bg_stock'] < $goods_number) return json(['status' => 0, 'msg' => '库存不足']);
        
        $user = Db::table('up_u_user')->where('user_id', $this->user_id)->field('user_bo_role, user_rule, user_bo_balance')->find();
        if($user['user_bo_role'] == 1 || $user['user_bo_role'] == 2 || $user['user_rule'] == '1' || $user['user_rule'] == '2'){
            return json(['status' => 0, 'msg' => '暂无购买权限']);
        }
        
        $order_number = 'BO_' . date('YmdHis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
        $total_price = $goods_number * $goods['bg_price'];
        if($total_price > $user['user_bo_balance']) return json(['status' => 222, 'msg' => '余额不足']);
        
        $insertOrder = [
            'bo_user_id' => $this->user_id,
            'bo_number' => $order_number,
            'bo_total_price' => $total_price,
            'bo_total_number' => $goods_number,
            'bo_remark' => $order_remark,
            'bo_addr_id' => $addr_id,
            'bo_create_time' => $this->create_time
        ];
        $insertInfo = [
            'boi_goods_id' => $bg_id,
            'boi_user_id' => $this->user_id,
            'boi_goods_number' => $goods_number,
            'boi_goods_price' => $total_price
        ];
        Db::startTrans();
        try{
            // 插入订单表
            $order_id = Db::table('up_bo_order')->insertGetId($insertOrder);
            
            // 插入订单明细
            $insertInfo['boi_order_id'] = $order_id;
            Db::table('up_bo_order_info')->insert($insertInfo);
            
            // 减库存&加销量
            Db::table('up_bo_goods')->where('bg_id', $bg_id)->update([
                    'bg_stock' => $goods['bg_stock'] - $goods_number,
                    'bg_sale_number' => $goods['bg_sale_number'] + $goods_number
                ]);
            
            Db::commit();
            return json(['status' => 200, 'msg' => $order_id]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 立即购买--购物车商品订单
     * @return \think\response\Json
     * car_ids         购物车ID，逗号隔开
     * addr_id         收货地址ID
     * order_remark    订单备注
     */
    public function commitShopCarOrder(){
        $car_ids = input('car_ids'); // 购物车ID，逗号隔开
        $addr_id = input('addr_id'); // 收货地址ID
        $order_remark = input('order_remark'); // 订单备注

        if(!$this->user_id) return json(['status' => 0, 'msg' => '请先登录']);
        if(!$car_ids) return json(['status' => 0, 'msg' => '请选择需要提交的购物车商品']);
        if(!$addr_id) return json(['status' => 0, 'msg' => '请选择收货地址']);
    
        $user = Db::table('up_u_user')->where('user_id', $this->user_id)->field('user_bo_role, user_rule, user_bo_balance')->find();
        if($user['user_bo_role'] == 1 || $user['user_bo_role'] == 2 || $user['user_rule'] == '1' || $user['user_rule'] == '2'){
            return json(['status' => 0, 'msg' => '暂无购买权限']);
        }
        
        $flag = ['status' => 1, 'msg' => '']; // 用于判断商品异常情况
        $total_price = 0; // 商品总价
        $total_number = 0; // 商品总数
        $order_info = []; // 订单详情
        $car = Db::table('up_bo_goods_car')
            ->join('up_bo_goods', 'bgc_goods_id = bg_id', 'LEFT')
            ->field('bg_status, bg_price, bg_stock, bg_sale_number, bgc_goods_number, bg_name, bg_id')
            ->where('bgc_id', 'IN', $car_ids)
            ->select();
        
        foreach($car as $k => $v){
            if($v['bgc_goods_number'] < 1){
                $flag['status'] = 0;
                $flag['msg'] = '商品' . $v['bg_name'] . '购买数量不可小于1';
                break;
            }
            if($v['bg_stock'] < $v['bgc_goods_number']){
                $flag['status'] = 0;
                $flag['msg'] = '商品' . $v['bg_name'] . '库存不足';
                break;
            }
            if($v['bg_status'] != 1){
                $flag['status'] = 0;
                $flag['msg'] = '商品' . $v['bg_name'] . '状态异常';
                break;
            }
            $total_number += $v['bgc_goods_number'];
            $total_price += $v['bgc_goods_number'] * $v['bg_price'];
            $order_info[$k] = [
                'boi_goods_id' => $v['bg_id'],
                'boi_user_id' => $this->user_id,
                'boi_goods_number' => $v['bgc_goods_number'],
                'boi_goods_price' => $v['bgc_goods_number'] * $v['bg_price']
            ];
        }
        if($flag['status'] == 0) return json($flag);
        if($total_price > $user['user_bo_balance']) return json(['status' => 222, 'msg' => '余额不足']);
        if($total_number == 0) return json(['status' => 0, 'msg' => '请选择商品']);
        
        $order_number = 'BO_' . date('YmdHis') . str_pad(mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
        $order = [
            'bo_user_id' => $this->user_id,
            'bo_number' => $order_number,
            'bo_total_price' => $total_price,
            'bo_total_number' => $total_number,
            'bo_remark' => $order_remark,
            'bo_addr_id' => $addr_id,
            'bo_create_time' => $this->create_time
        ];
        
        Db::startTrans();
        try{
            // 插入订单
            $order_id = Db::table('up_bo_order')->insertGetId($order);
            
            // 插入订单详情
            foreach($order_info as $ko => $vo){
                $order_info[$ko]['boi_order_id'] = $order_id;
            }
            Db::table('up_bo_order_info')->insertAll($order_info);
            
            // 减库存&加销量
            foreach($car as $kc => $vc){
                $update = [
                    'bg_id' => $vc['bg_id'],
                    'bg_stock' => $vc['bg_stock'] - $vc['bgc_goods_number'],
                    'bg_sale_number' => $vc['bg_sale_number'] + $vc['bgc_goods_number']
                ];
                Db::table('up_bo_goods')->update($update);
            }
            
            // 删除购物车
            Db::table('up_bo_goods_car')->where('bgc_id', 'IN', $car_ids)->delete();
            
            Db::commit();
            return json(['status' => 200, 'msg' => $order_id]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 支付订单
     * @return \think\response\Json
     * order_id     订单ID
     */
    public function payOrder(){
        $order_id = input('order_id');
        $order = Db::table('up_bo_order')
            ->join('up_u_user', 'user_id = bo_user_id', 'LEFT')
            ->where('bo_id', $order_id)
            ->field('bo_total_price, bo_status, bo_user_id, user_bo_total_balance, user_bo_balance, 
            user_bo_first_balance, user_bo_huo_balance, user_bo_other_balance, user_bo_role, user_rule')
            ->find();
        if($order['bo_status'] != 1) return json(['status' => 0, 'msg' => '订单状态异常']);
        if($order['bo_total_price'] > $order['user_bo_balance']) return json(['status' => 222, 'msg' => '余额不足']);
        if($order['user_bo_role'] == 1 || $order['user_bo_role'] == 2 || $order['user_rule'] == '1' || $order['user_rule'] == '2'){
            return json(['status' => 0, 'msg' => '暂无购买权限']);
        }
        
        $order_info = Db::table('up_bo_order_info')
            ->join('up_bo_goods', 'bg_id = boi_goods_id', 'LEFT')
            ->where('boi_order_id', $order_id)
            ->field('bg_name, bg_status')
            ->select();
        $flag = ['status' => 1, 'msg' => ''];
        foreach($order_info as $k => $v){
            if($v['bg_status'] != 1){
                $flag['status'] = 0;
                $flag['msg'] = '商品' . $v['bg_name'] . '状态异常';
                break;
            }
        }
        if($flag['status'] == 0) return json($flag);
        
        $update = [
            'bo_id' => $order_id,
            'bo_is_pay' => 2,
            'bo_pay_type' => 1,
            'bo_status' => 2,
            'bo_pay_time' => $this->create_time
        ];

        $update_price = $this->decPrice($order);

        Db::startTrans();
        try{
            // 更新订单状态
            Db::table('up_bo_order')->update($update);
            
            // 更新用户积分
            Db::table('up_u_user')->where('user_id', $order['bo_user_id'])->update($update_price);
            
            // 插入积分明细
            $this->insertBoInte($order['bo_user_id'], 2, '购买商品', $order['bo_total_price'], $order['user_bo_balance'], $order_id);

            Db::commit();
            return json(['status' => 200, 'msg' => $order_id, 'use_balance' => $order['bo_total_price'], 'user_balance' => $update_price['user_bo_balance']]);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }
    
    //*************************************  公共方法
    /**
     * 商品列表
     * @param $keyword         搜索关键字
     * @param $first_cate      一级分类
     * @param $second_cate     二级分类
     * @param $is_recommend    是否推荐   1 不推荐  2 推荐
     * @param $bg_type         1 花样青青   2 脖脸皮
     * @return bool|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function goodsList($keyword = '', $first_cate = 0, $second_cate = 0, $is_recommend = 0, $bg_type = 0){
        $where['bg_status'] = 1;
        if($keyword != ''){
            $where['bg_name'] = ['like', '%' . $keyword . '%'];
        }
        if($first_cate != 0){
            $where['bg_first_cate'] = $first_cate;
        }
        if($second_cate != 0){
            $where['bg_second_cate'] = ['LIKE', '%,' . $second_cate . ',%'];
        }
        if($is_recommend != 0){
            $where['bg_is_commend'] = $is_recommend;
        }
        if($bg_type != 0){
            $where['bg_type'] = $bg_type;
        }

        $query = Db::table('up_bo_goods')
            ->where($where)
            ->field('bg_id, bg_name, bg_thumb, bg_spec, bg_stock, bg_price, bg_sale_number, 
            bg_show_salenumber, bg_show_stock')
            ->order('bg_sort desc, bg_time desc');
        if($is_recommend == 2){
            $query->limit(5);
        }else{
            $query->page($this->page, 10);
        }
        $data = $query->select();
        return $data;
    }
    
    /**
     * 我的购物车
     * @param $use_type    1 统计购物车数量   2 购物车列表
     * @return false|int|\PDOStatement|string|\think\Collection
     */
    private function myShopCar($use_type){
        $query = Db::table('up_bo_goods_car')
            ->join('up_bo_goods', 'bgc_goods_id = bg_id', 'LEFT')
            ->where(['bgc_user_id' => $this->user_id, 'bg_status' => 1]);
        if($use_type == 1){ // 统计数量
            $data = $query->count();
        }else{
            $data = $query->field('bgc_id, bg_id, bg_name, bg_spec, bg_price, bgc_goods_number, 
            bg_thumb, bg_stock')
                ->order('bgc_time desc')
//                ->page($this->page)
                ->select();
        }
        return $data;
    }
    
    /**
     * 是否收藏脖脸皮商品
     * @param $bg_id    商品ID
     * @return int
     */
    private function isCollectGoods($bg_id){
        $isCollect = Db::table('up_bo_collect_goods')
            ->where(['bcg_user_id' => $this->user_id, 'bcg_goods_id' => $bg_id])
            ->find();
        if($isCollect){
            $collect = 1; // 已收藏
        }else{
            $collect = 2; // 未收藏
        }
        return $collect;
    }
    
    /**
     * 默认快递公司
     * @return mixed
     */
    private function expressCompany(){
        return Db::table('up_about')->where('id', 7)->value('content');
    }

    /**
     * 根据扣款顺序，生成用户剩余积分的更新数组
     * @param $order     订单信息
     * @return array
     */
    private function decPrice($order){
        $update_price = [
            'user_bo_balance' => $order['user_bo_balance'],
            'user_bo_first_balance' => $order['user_bo_first_balance'],
            'user_bo_huo_balance' => $order['user_bo_huo_balance'],
            'user_bo_other_balance' => $order['user_bo_other_balance']
        ];
        // 扣款顺序，首款>货款
        if($order['bo_total_price'] <= $order['user_bo_first_balance']){
            $update_price['user_bo_first_balance'] = $order['user_bo_first_balance'] - $order['bo_total_price'];
        }else{
            $update_price['user_bo_first_balance'] = 0;
            $cha1 = $order['bo_total_price'] - $order['user_bo_first_balance'];
            if($cha1 <= $order['user_bo_huo_balance']){
                $update_price['user_bo_huo_balance'] = $order['user_bo_huo_balance'] - $cha1;
            }else{
                $update_price['user_bo_huo_balance'] = 0;
                $cha2 = $cha1 - $order['user_bo_huo_balance'];
                if($cha2 <= $order['user_bo_other_balance']){
                    $update_price['user_bo_other_balance'] = $order['user_bo_other_balance'] - $cha2;
                }else{
                    $update_price['user_bo_other_balance'] = 0;
//                    return json(['status' => 0, 'msg' => '可用积分不足']);
                }
            }
        }
        $update_price['user_bo_balance'] = $order['user_bo_balance'] - $order['bo_total_price'];

        return $update_price;
    }
}